/*
 * @Author: your name
 * @Date: 2021-11-10 08:34:30
 * @LastEditTime: 2021-12-28 10:14:59
 * @LastEditors: Please set LastEditors
 * @Description: 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 * @FilePath: \li-yuwei\egg-example\app\service\login.js
 */
const Service = require("egg").Service;
const timeFormart = require("../extends/timeFormart");
class BrokerService extends Service {
    //获取列表
    async getBrokerList() {
            const { ctx, app } = this;
            let res = await app.mysql.select("broker");
            let total = res.length;
            res = res.map((item, index) => {
                return {
                    ...item,
                    key: index + 1,
                };
            });
            return {
                total,
                res,
            };
        }
        //搜索
    async searchBrokerList() {
            const { ctx, app } = this;
            const { keyword, state } = ctx.query;
            let res = await app.mysql.select("broker");
            if (keyword) {
                res = res.filter((item) => item.telephone.includes(keyword) || item.name.includes(keyword));
            }
            if (state !== undefined) {
                res = res.filter((item) => item.state - 0 === state - 0);
            }
            return res;
        }
        //删除
    async delBrokerList() {
            const { ctx, app } = this;
            const { id } = ctx.params;
            const res = await app.mysql.delete("broker", { id });
            return res;
        }
        //添加
    async addBrokerList() {
            const { ctx, app } = this;
            const { name, telephone, sex, company, address, detail_address } = ctx.request.body;
            const res = await app.mysql.insert("broker", {
                ...ctx.request.body,
                create_time: timeFormart(Date.now()),
            });
            return res;
        }
        //编辑
    async editBrokerList() {
            const { ctx, app } = this;
            const { id, name, telephone, sex, company, address, state, detail_address } = ctx.request.body;
            const res = await app.mysql.update("broker", { id, ...ctx.request.body });
            return res;
        }
        //启用
    async startBrokerState() {
            const { ctx, app } = this;
            const { id } = ctx.params;
            const res = await app.mysql.update("broker", { id, state: 1 });
            return res;
        }
        //禁用
    async stopBrokerState() {
            const { ctx, app } = this;
            const { id } = ctx.params;
            const res = await app.mysql.update("broker", { id, state: 0 });
            return res;
        }
        //发送验证码
    async getBrokerVCode() {
            const { ctx, app } = this;
            const { telephone } = ctx.request.body;
            const res = await app.mysql.get("broker", { telephone });
            if (res) {
                //生成随机验证码
                const vCode = Math.random().toString().slice(2, 6);
                return vCode;
            } else {
                return "经纪人手机号不存在";
            }
        }
        //获取经纪人信息
    async getBrokerInfo() {
        const { ctx, app } = this;
        const { telephone } = ctx.query;
        const res = await app.mysql.get("broker", { telephone });
        return res;
    }
}
module.exports = BrokerService;